﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Common;

public partial class shopping_ordereditor : MemberPageBase
{
    public string OrderNumber = "", OrderDate = "", TotalPrice = "", OrderStatus = "", OrderTypeName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "submitorder")
        {
            SubmitOrderInfo();
        }

        if (!IsPostBack)
        {
            Initialize(); 
        }
    }

    #region 对象初始化
    void Initialize()
    {
        string result;
        Model.OrderBase _order = BLL.OrderBase.GetObjectById(RequestBase.GetQueryString("orderid"), out result);

        if (_order.StatusCode != 0)
            Response.Redirect("shopping_orderlist.aspx");
        OrderObj = _order;
    }
    #endregion
      
    #region 对象访问
    /// <summary>
    /// 订单实体
    /// </summary>
    Model.OrderBase OrderObj
    {
        get
        {
            Model.OrderBase _orderObj = new Model.OrderBase();

            _orderObj.OrderId = RequestBase.GetFormString("txtOrderId");
            _orderObj.ShippingAddressId = RequestBase.GetFormString("txtShippingAddressId");
            _orderObj.ShipAddress = RequestBase.GetFormString("hidShipAddress");
            _orderObj.ShipCounties = RequestBase.GetFormString("hidShipCounties").Split('|')[2];
            _orderObj.ShipCity = RequestBase.GetFormString("hidShipCounties").Split('|')[1];
            _orderObj.ShipState = RequestBase.GetFormString("hidShipCounties").Split('|')[0];
            _orderObj.ShipCountry = "中国";
            _orderObj.ShipZip = RequestBase.GetFormString("hidShipZip");
            _orderObj.ShipTel = RequestBase.GetFormString("hidShipTel");
            _orderObj.ShipMobile = RequestBase.GetFormString("hidShipMobile");
            _orderObj.ShipToName = RequestBase.GetFormString("hidShipToName");
            _orderObj.UserId = this.UserId;
            _orderObj.StatusCode = 0;
            _orderObj.PaymentTypeId = 0;
            _orderObj.PaymentType = "";
            _orderObj.NeedInvoice = false;
            _orderObj.InvoiceTitle = "";
            _orderObj.InvoiceType = "";
            _orderObj.Locale = "中文";
            _orderObj.CreatedBy = this.UserId;
            _orderObj.ModifyBy = this.UserId;
            _orderObj.CreatedOn = DateTime.Now;
            _orderObj.ModifyOn = DateTime.Now;
            _orderObj.ShippingCost = 0;
            _orderObj.ShippingMethod = "快递";
            _orderObj.ShippingMethodType = 1;
            _orderObj.TotalPrice = decimal.Parse(txtTotalCost.Value.Trim());
            _orderObj.OrderDate = DateTime.Now;
            _orderObj.OrderNumber = txtOrderNumber.Value.Trim();
            _orderObj.OrderItems = Profile.OrderItems.GetOrderItems(_orderObj.OrderId);
            _orderObj.Comment = RequestBase.GetFormString("txtComments");
            return _orderObj;
        }
        set
        {
            if (value != null)
            {
                OrderNumber = value.OrderNumber;
                OrderDate = value.OrderDate.ToString("yyyy-MM-dd");
                OrderStatus = value.StatusCode.ToString().Replace("-1", "已作废").Replace("0", "待支付").Replace("1", "已付款");
                txtOrderId.Value = value.OrderId;
                TotalPrice = value.TotalPrice.ToString(); 
                txtShippingAddressId.Value = value.ShippingAddressId; 
                txtTotalCost.Value = value.TotalPrice.ToString();
                txtOrderNumber.Value = value.OrderNumber;
                OrderTypeName = value.OrderTypeCode.ToString().Replace("1", "普通订单").Replace("2", "团购订单");

                foreach (Model.OrderItems _item in value.OrderItems)
                {
                    Profile.OrderItems.Remove(_item.ProductId + "|" + _item.OrderId);
                    Profile.Save();
                }

                foreach (Model.OrderItems _item in value.OrderItems)
                {
                    Profile.OrderItems.Add(_item.ProductId + "|" + _item.OrderId, _item.OrderId, _item.Quantity, _item.UnitPrice);
                    Profile.Save();
                }
            }
        }
    }
    #endregion

    #region 保存数据

    void SubmitOrderInfo()
    {
        Model.OrderBase _order = OrderObj;
        string res = BLL.OrderBase.OrderModify(_order);

        switch (res.Split('|')[0])
        {
            case "succeeded":
                foreach (Model.OrderItems _item in _order.OrderItems)
                {
                    Profile.OrderItems.Remove(_item.ProductId + "|" + _item.OrderId);
                    Profile.Save();
                }
                JScript.AlertAndRedirect("保存成功!", "shopping_orderlist.aspx");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}